package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.*;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//套餐模块
@Service    //将service交给ioc容器
@Transactional      //事务管理器
public class SetmealServiceImpl implements SetmealService {

    //依赖注入mapper层
    @Autowired
    private SetmealMapper setmealMapper;

    //依赖注入mapper层
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    //依赖注入mapper层
    @Autowired
    private CategoryMapper categoryMapper;

    //依赖注入mapper层
    @Autowired
    private DishMapper dishMapper;

    //依赖注入service层
    @Autowired
    private DishService dishService;

    /*//依赖注入mapper层
    @Autowired
    private DishFlavorMapper dishFlavorMapper;*/


    ////套餐查询 + 模糊查询
    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        //业务处理
        //1. 设置模糊查询的条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotBlank(name),Setmeal::getName, name);       //必须进行判空
        //2. 开启分页
        Page<Setmeal> page = new Page<>(pageNum,pageSize);
        //3. 调用mp进行查询
        page = setmealMapper.selectPage(page,qw);

        //补全实体类信息
        //遍历
        for (Setmeal setmeal : page.getRecords()) {
            //4.根据categotyId查询分类表,得到分类名
            Category category = categoryMapper.selectById(setmeal.getCategoryId());
            //4-1 将分类名封装到菜品中
            setmeal.setCategoryName(category.getName());
            //5.根据id查询到setmealDish表
            List<SetmealDish> dishList = setmealDishMapper.selectList(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, setmeal.getId()));
            //5-1 将口味列表封装到菜品中
            setmeal.setSetmealDishes(dishList);
        }
        //6. 返回结果
        return page;
    }


    //回显菜品信息 + 姓名模糊匹配
    @Override
    public List<Dish> findByCategoryIdOrName(Long categoryId,String name) {
        //业务处理
        //1.创建条件构造器
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(categoryId!=null,Dish::getCategoryId, categoryId);
        qw.like(StrUtil.isNotBlank(name),Dish::getName,name);
        qw.eq(Dish::getStatus,1);   //状态码为1
        //调用mp查询dish表
        return dishMapper.selectList(qw);
    }


    //新增套餐信息
    @Override
    public void save(Setmeal setmeal) {
        //业务处理
        //1.直接存入套餐
        setmealMapper.insert(setmeal);
        //2.存套餐中的菜品信息
        //2-1 遍历
        for (SetmealDish dish : setmeal.getSetmealDishes()) {
            //2-2关联信息
            dish.setSetmealId(setmeal.getId());
            //2-3将菜品存入setmealDish表
            setmealDishMapper.insert(dish);
        }
    }



    //回显套餐信息
    @Override
    public Setmeal findById(Long id) {
        //业务处理
        //1.根据id查询setmeal
        Setmeal setmeal = setmealMapper.selectById(id);
        //2.根据id查询setmealDish表
        List<SetmealDish> dishList = setmealDishMapper.selectList(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, id));
        //3. 将菜品表封装到setmeal中
        setmeal.setSetmealDishes(dishList);
        return setmeal;
    }

   /* //菜品名模糊查询
    @Override
    public List<Dish> findByName(String name) {
        //业务处理
        //1.创建条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(Dish::getName,name);
        //2.调用mp查询dish表
        return dishMapper.selectList(qw);
    }*/


    //修改套餐信息
    @Override
    public void updateById(Setmeal setmeal) {
        //业务处理
        //1.调用mp进行更新
        setmealMapper.updateById(setmeal);
        //2.修改setmealDish表中的数据
        //2-1先删
        setmealDishMapper.delete(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId,setmeal.getId()));
        //2-2在新增
        for (SetmealDish setmealDish : setmeal.getSetmealDishes()) {
            //关联id
            setmealDish.setSetmealId(setmeal.getId());
            //新增菜品
            setmealDishMapper.insert(setmealDish);
        }
    }



    //(批量)停售、起售
    @Override
    public void setStatus(Integer status, List<Long> ids) {
        //业务处理
        //1.创建条件构造器
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        //2. 设置条件
        qw.in(Setmeal::getId,ids);
        //3.创建对象,设置status
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        //4.调用mp查询
        setmealMapper.update(setmeal,qw);
    }

    //(批量)删除
    @Override
    public void deleteByIds(List<Long> ids) {
        //业务处理
        //1.查询ids中status==1的值
        Integer count = setmealMapper.selectCount(new LambdaQueryWrapper<Setmeal>().in(Setmeal::getId, ids).eq(Setmeal::getStatus, 1));
        //2.判断是否为1
        if (count==1){
            throw new CustomException("删除失败:此套餐在售无法删除...");
        }

        //3.若不为起售状态
        //3-1先删setmeal
        setmealMapper.deleteBatchIds(ids);
        //3-2再删setmealDish
        setmealDishMapper.delete(new LambdaQueryWrapper<SetmealDish>().in(SetmealDish::getSetmealId,ids));
    }

    //c端查询套餐列表
    @Override
    public List<Setmeal> findList(String categoryId, String status) {
        //业务处理
        //1.创建条件构造器,并设置条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId);
        qw.eq(Setmeal::getStatus,status);
        //2.调用mp查询,并返回结果
        return setmealMapper.selectList(qw);
    }

    //c端查询套餐中菜品信息
    @Override
    public List<Dish> showDishMsg(Long id) {
        //业务处理
        //0.新建集合用于存储菜品信息
        List<Dish> dishList = new ArrayList<>();

        //1.根据id查询setmealDish表
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, id));
        //2.遍历
        for (SetmealDish setmealDish : setmealDishList) {
            //2-1根据dishId查询dish表
            Dish dish = dishService.findById(setmealDish.getDishId());
            //2-2将菜品数设置到菜品中
            dish.setCopies(setmealDish.getCopies());
            //2-3将菜品添加到集合中
            dishList.add(dish);
        }

        return dishList;
    }


    /*@Test
    public void test01() throws Exception{
        Dish dish = dishService.findById(1413342036832100354L);
        System.out.println(dish);
    }*/
}
